home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / egebook.lha / ege.book / 5 / Collection.C < prev    next >
C/C++ Source or Header  |  1992-06-07  |  2KB  |  96 lines

  1. #include <iostream.h>
  2. #include "Collection.h"
  3. #include "Professor.h"
  4. #include "Course.h"
  5. #include "TeachingAssistant.h"
  6.  
  7. // body for class Collection
  8.  
  9. int Collection::GrowIncrement = 5;
  10.  
  11. void Collection::add(Example *element) {
  12.   if (filled >= size) {
  13.     Example **oldlist = list;
  14.     list = new (Example *[size + GrowIncrement]);
  15.     for (int i = 0; i < size; i++)
  16.       list[i] = oldlist[i];
  17.     size += GrowIncrement;
  18.   };
  19.   list[filled++] = element;
  20. };
  21.  
  22. Example* Collection::search(String &key) {
  23.   for (int i = 0; i < filled; i++)
  24.     if (list[i]->identify(key))
  25.       return list[i];
  26.   return NULL;
  27. };
  28.  
  29. Example* Collection::search(Example *key) {
  30.   for (int i = 0; i < filled; i++)
  31.     if (list[i] == key)
  32.       return list[i];
  33.   return NULL;
  34. };
  35.  
  36. void Collection::remove(Example *element) {
  37.   int j = 0;
  38.   Example **oldlist = list;
  39.   for (int i = 0; i < filled; i++)
  40.     if (oldlist[i] != element)
  41.       list[j++] = oldlist[i];
  42.   filled = j;
  43. };
  44.  
  45. void Collection::print() {
  46.   if (filled == 0)
  47.     cout << "collection is empty\n";
  48.   else
  49.     for (int i = 0; i < filled; i++)
  50.       list[i]->print();
  51. };
  52.  
  53. void Collection::maintain(){
  54.   String selection, answer;
  55.   Example *sel_example;
  56.  
  57.   while (TRUE) {
  58.     print();
  59.     cout << "Select an entry or enter 0 to exit: ";
  60.     cin >> selection;
  61.     if (selection == "0") break;
  62.     if ((sel_example = search(selection)) != NULL) {
  63.       sel_example->print();
  64.       cout << "maintain (m) or delete (d) ? ";
  65.       cin >> selection;
  66.       if (selection.contains("d"))
  67.         remove(sel_example);
  68.       else
  69.         sel_example->maintain();
  70.     } else {
  71.       cout << "not found, do you want to create it (y/n) ? ";
  72.       cin >> answer;
  73.       if (answer.contains("y"))
  74.         add_alike(selection);
  75.     };
  76.   };
  77. };
  78.  
  79. Example* Collection::select() {
  80.   String selection;
  81.   print();
  82.   cout << "enter name: ";
  83.   cin >> selection;
  84.   return(search(selection));
  85. };
  86.  
  87. void Collection::add_alike(String &name) {
  88.   if (filled == 0)
  89.     cout << "sorry, list is empty, cannot add\n";
  90.   else {
  91.     Example *element = list[0]->alike(name);
  92.     if (!search(element))
  93.        add(element);
  94.   };
  95. };
  96.